package com.vf.admin.utils.douyin;



import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class VideoDownload {


    public static void main(String[] args) {

    }

//https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200f5c0000br35vevrri6d3j3fm8h0&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH
//http://v6-dy.ixigua.com/0ec384f1212dc14bd070b5c307c0eb51/5ee34ba3/video/tos/cn/tos-cn-ve-15/cf8a3327bba240b7bee0240a81d83ef3/?a=1128&br=3018&bt=1006&cr=0&cs=0&dr=0&ds=6&er=&l=202106121631500100190171340A08E92C&lr=&mime_type=video_mp4&qs=0&rc=ajdkZGhkNWt0dTMzZWkzM0ApaWdpZjpoNmU8N2Q1aGRnZmdsMnNqZ3BsbjBfLS0zLS9zc2IuNDJhYV80YmJeMTYuX146Yw%3D%3D&vl=&vr=
//https://api.amemv.com/aweme/v1/play/?video_id=v0200f6b0000br3qqho2sajemava54og&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0&is_play_url=1&is_support_h265=0&source=PackSourceEnum_PUBLISH
//    public static void main(String[] args) {
//        httpDownload1111111("http://v26-dy.ixigua.com/a97d74dca65e6886a549e2359df51af2/5ee340c8/video/tos/cn/tos-cn-ve-15/a12b3f7412e046398c6e5e9ac0ac4815/?a=1128&br=4170&bt=1390&cr=0&cs=0&dr=0&ds=6&er=&l=202106121545360100140460130D011938&lr=&mime_type=video_mp4&qs=0&rc=MztpbWdsdWg4dTMzZmkzM0ApMzY4OTo6N2QzNzQ0N2k4O2dlajAwM29zczBfLS0yLS9zczBjLi41LjVeMDNfLzYuMzQ6Yw%3D%3D&vl=&vr=","D:/123.mp4");
//    }
//
//    public static   boolean httpDownload1111111(String httpUrl, String saveFile) {
//        String dir = saveFile.substring(0,saveFile.lastIndexOf("/")+1);
//        File fd = new File(dir);
//        if (!fd.exists()) {// 路径不存在,新建
//            fd.mkdirs();
//        }
//        // 1.下载网络文件
//        int byteRead;
//        URL url;
//        try {
//            url = new URL(httpUrl);
//        } catch (MalformedURLException e1) {
//            e1.printStackTrace();
//            return false;
//        }
//        InputStream inStream = null;
//        FileOutputStream fs = null;
//        try {
//            //2.获取链接
//            URLConnection conn = url.openConnection();
//            conn.setRequestProperty("User-Agent","(Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36");
//
//
//            //3.输入流
//            inStream = conn.getInputStream();
//            //3.写入文件
//            fs = new FileOutputStream(saveFile);
//
//            byte[] buffer = new byte[1024];
//            while ((byteRead = inStream.read(buffer)) != -1) {
//                fs.write(buffer, 0, byteRead);
//            }
//
//            return true;
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//            return false;
//        } catch (IOException e) {
//            e.printStackTrace();
//            return false;
//        }finally{
//            try{
//                if(inStream!=null){
//                    inStream.close();
//                }
//                if(fs!=null){
//                    fs.close();
//                    fs.flush();
//                }
//            }catch (IOException e) {
//                e.printStackTrace();
//            }
//
//        }
//    }



	public  boolean httpDownload(String httpUrl, String saveFile) {
		String dir = saveFile.substring(0,saveFile.lastIndexOf("/")+1);
		File fd = new File(dir);
		if (!fd.exists()) {// 路径不存在,新建
			fd.mkdirs();
		}
        // 1.下载网络文件
        int byteRead;
        URL url;
        try {
            url = new URL(httpUrl);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return false;
        }
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            //2.获取链接
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("User-Agent","(Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36");


            //3.输入流
            inStream = conn.getInputStream();
            //3.写入文件
            fs = new FileOutputStream(saveFile);
 
            byte[] buffer = new byte[1024];
            while ((byteRead = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteRead);
            }
            
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
            try{
                if(inStream!=null){
                	inStream.close();  
                }
                if(fs!=null){
                	fs.close();  
                	fs.flush(); 
                }
           }catch (IOException e) { 
                e.printStackTrace();  
           }

       }
    }




}
